package com.example.demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author 兴趣使然黄小黄
 * @version 1.0
 * @date 2023/7/26 12:41
 */
@RestController
@RequestMapping("/user")
public class UserController {

    private final static String SESSION_KEY = "USER_SEESION_KEY";

    @RequestMapping("/login")
    public String login(HttpSession session) {
        // 存储 session 到 redis
        session.setAttribute(SESSION_KEY, "xiaohuang");
        return "login success";
    }

    @RequestMapping("/get")
    public String get(HttpServletRequest request) {
        String username = "暂无";
        HttpSession session = request.getSession(false);
        if (session != null) {
            username = (String) session.getAttribute(SESSION_KEY);
        }
        return username;
    }
}
